package com.wxsong.authplatform.authweb.controller.main;

import com.google.common.collect.Maps;
import com.wxsong.authplatform.authweb.util.ListResponse;
import com.wxsong.authplatform.authweb.util.Response;
import com.wxsong.authplatform.authweb.util.ResponseCode;
import com.wxsong.authplatform.authweb.controller.common.Const;
import com.wxsong.authplatform.authweb.model.menu.Menu;
import com.wxsong.authplatform.authweb.model.users.Users;
import com.wxsong.authplatform.authweb.service.menu.MenuService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/main")
public class MainController {

    private Logger logger = LoggerFactory.getLogger(MainController.class);

    @Resource
    private MenuService menuService;

    @RequestMapping("/index")
    public String index(ModelMap modelMap){
        Subject subject = SecurityUtils.getSubject();
        //获取当前用户拥有的主菜单的权限
        try {
            String name = subject.getPrincipal().toString();
            modelMap.put("name", name);
            Object cur = subject.getSession().getAttribute(Const.SESSION_USER_KEY);
            if (null == cur) {
                logger.error("用户已登出");
                return "redirect:/login/logout";
            }
            Users users = (Users) cur;
            long curUserId = users.getId();

            List<Menu> menus = menuService.findOwnFirstMenu(curUserId);
            modelMap.put("menus", menus);
        }catch (Exception e){
            logger.error("获取菜单数据异常，", e);
        }
        return "main/index";
    }

    @RequestMapping("/menu/data")
    @ResponseBody
    public Response menuData(String parent){
        logger.info("进入获取菜单数据的方法，接收到参数：" + parent);
        Map resultMap = Maps.newHashMap();
        try {
            if(StringUtils.isNotBlank(parent)){  //默认为一级菜单，根菜单ID为3
                parent = URLDecoder.decode(parent, "UTF-8");
            }
            Subject subject = SecurityUtils.getSubject();
            Object cur = subject.getSession().getAttribute(Const.SESSION_USER_KEY);
            if(null == cur) {
                throw new Exception("用户已登出");
            }
            Users users = (Users) cur;
            long curUserId = users.getId();
            List<Menu> menuList = menuService.getByParentCode(parent, curUserId);
            resultMap.put("menuList", menuList);
            return ListResponse.success(Long.valueOf(menuList.size()+""), menuList);
        }catch (Exception e){
            logger.error("获取菜单数据异常，", e);
        }
        return ListResponse.fail(ResponseCode.FAIL.info);
    }
}
